﻿namespace RialternativeSample.Server.Infrastructure
{
    using System;
    using Microsoft.Practices.Unity;
    using Rialternative.Infrastructure;

    /// <summary>
    /// Provides some helps for using unity.
    /// </summary>
    public class UnityProvider : IDIContainer
    {
        /// <summary>
        /// Object locker for singleton pattern.
        /// </summary>
        private static object syncRoot = new object();

        /// <summary>
        /// The UnityProvider instance.
        /// </summary>
        private static volatile UnityProvider instance;

        /// <summary>
        /// The unity container.
        /// </summary>
        private readonly IUnityContainer container = new UnityContainer();

        /// <summary>
        /// Prevents from creating a default instance of the <see cref="UnityProvider"/> class.
        /// </summary>
        private UnityProvider() { }

        /// <summary>
        /// Gets the unity container.
        /// </summary>
        /// <value>The unity container.</value>
        public static UnityProvider Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                    {
                        instance = new UnityProvider();
                    }
                }

                return instance;
            }
        }

        public IUnityContainer UnityContainer
        {
            get
            {
                return this.container;
            }
        }

        public object ResolveType(Type type)
        {
            return container.Resolve(type);
        }

        public void RegisterType(Type type)
        {
            this.container.RegisterType(type);
        }

        public void RegisterTypePropertyInjection(Type type, string propertyName, object propertyValue)
        {
            this.container.RegisterType(type, new InjectionProperty(propertyName, propertyValue));
        }
    }
}
